package com.hyfrogx.modules.manage.service.course.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.modules.app.entity.User;
import com.hyfrogx.modules.manage.dao.course.CourseManageDao;
import com.hyfrogx.modules.manage.entity.course.CourseManageEntity;
import com.hyfrogx.modules.manage.entity.course.CourseOrderEntity;
import com.hyfrogx.modules.manage.service.course.CourseManageService;
import com.hyfrogx.modules.manage.service.course.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 罗春杰-技术部
 */
@Service
public class CourseManageServiceImpl extends ServiceImpl<CourseManageDao, CourseManageEntity> implements CourseManageService {


    @Autowired
    @Lazy
    CourseOrderService orderService;

    @Override
    public Page selectManagePage(QueryExt queryExt) {
        Map map = new HashMap();
        map.putAll(queryExt.getCondition());
        queryExt.setRecords(this.baseMapper.ListGetByPage(queryExt,map));
        return queryExt;
    }

    @Override
    public Page listCourseManageOnline(QueryExt queryExt) {
        Map map = new HashMap();
        map.putAll(queryExt.getCondition());
        List<CourseManageEntity> selectOnline = this.baseMapper.selectOnline(queryExt, map);
        if (map.get("uid") != null) {
            for (CourseManageEntity hyCourseManage : selectOnline) {
                hyCourseManage.setIsBuy(0);
                // 如果有订单，则显示课程已被购买
                int result = orderService.count(new QueryWrapper<CourseOrderEntity>()
                        .eq("uid",map.get("uid")).eq("cid",hyCourseManage.getId())
                        .eq("status",1));
                if (result > 0) {
                    hyCourseManage.setIsBuy(1);
                }
            }
        }
        queryExt.setRecords(selectOnline);
        if (selectOnline != null) {
            return queryExt;
        } else {
            throw  new MyException("暂无课程");
        }
    }

    @Override
    public CourseManageEntity getCourseById(Integer id) {
        return this.baseMapper.selectManageById(id);
    }
}
